package com.easy.dfcapi.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.core.web.Api20Controller;
import com.easy.dfcapi.entity.ImgReq;
import com.easy.dfcapi.entity.VideoReq;
import com.easy.dfcapi.res.Response;
import com.easy.dfcapi.service.ApiUploadService;
import com.easy.dfcapi.utils.ApiSessionUser;
import com.easy.dfcapi.utils.ServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 用户相关控制器
 */

@Controller
@RequestMapping(value = "${apiPath}/2.0/upload")
public class ApiUploadController extends Api20Controller {


    @Autowired
    private ApiUploadService uploadService;


    /**
     * 上传产品图片
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UploadImg", method = RequestMethod.POST)
    public Result UploadImg(Response<ImgReq> response, HttpServletRequest request) {

        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            return uploadService.UploadImg(response, request);

        } catch (IOException e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.UPLOAD_FAILD);
            return rt;

        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 上传视频
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UploadVideo", method = RequestMethod.POST)
    public Result UploadVideo(Response<VideoReq> response, HttpServletRequest request) {

        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                Result rt = new Result();
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            return uploadService.UploadVideo(response, request);

        } catch (IOException e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.UPLOAD_FAILD);
            return rt;

        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }


}
